﻿import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'controller.dart';
import 'modal.dart';

class PullRefreshList<T> extends StatefulWidget {
  final Future<ListResponse<T>> Function(int pageCount, int pageSize) fetchData;
  final Widget Function(T item) itemBuilder;
  final int pageSize;
  final int pageCount;
  final Widget? emptyWidget;
  final String? tag;
  final void Function(PullRefreshController<T> controller)? onControllerReady;

  final Header? header;
  final Footer? footer;

  const PullRefreshList({
    super.key,
    required this.fetchData,
    required this.itemBuilder,
    this.pageSize = 10,
    this.pageCount = 1,
    this.emptyWidget,
    this.tag,
    this.onControllerReady,
    this.header,
    this.footer,
  });

  @override
  State<PullRefreshList<T>> createState() => _PullRefreshListState<T>();
}

class _PullRefreshListState<T> extends State<PullRefreshList<T>> {
  late ScrollController _scrollController;
  late PullRefreshController<T> controller;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    controller = Get.put(
      PullRefreshController<T>(
        fetchData: widget.fetchData,
        pageSize: widget.pageSize,
        pageCount: widget.pageCount,
      ),
      tag: widget.tag,
      permanent: false,
    );
    widget.onControllerReady?.call(controller);
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (controller.dataList.isEmpty) {
        controller.initData();
      }
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (controller.dataList.isEmpty && controller.isRefreshing.value) {
        return const Center(child: CircularProgressIndicator());
      }

      if (controller.dataList.isEmpty) {
        return widget.emptyWidget ?? const Center(child: Text('暂无数据'));
      }

      return EasyRefresh(
        onRefresh: () async {
          await controller.refreshData();
        },
        onLoad: () async {
          if (controller.hasMoreData) {
            await controller.loadMoreData();
          } else {
            return IndicatorResult.noMore;
          }
        },
        header: widget.header ??
            const ClassicHeader(
              armedText: '释放刷新',
              readyText: '正在加载...',
              processedText: '加载完成',
              processingText: '正在刷新...',
              messageText: '更新于 %T',
              failedText: '刷新失败',
              noMoreText: '没有更多数据',
              dragText: '拉动刷新',
            ),
        footer: widget.footer ??
            const ClassicFooter(
              dragText: '上拉以加载',
              armedText: '释放准备加载',
              readyText: '正在加载...',
              processingText: '正在加载...',
              processedText: '加载成功',
              noMoreText: '没有更多数据',
              failedText: '加载失败',
              messageText: '最后更新于 %T',
            ),
        child: ListView.builder(
          controller: _scrollController,
          itemCount: controller.dataList.length,
          itemBuilder: (context, index) {
            return widget.itemBuilder(controller.dataList[index]);
          },
        ),
      );
    });
  }
}

